/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package probability;
import java.util.*;
import structure.*;
import util.*;

/**
 *
 * @author antonio
 */
public class ModifierPosition {

    private Vector totalInfoPoint;  //vettore contenente le info 
                                   //(nome_punto, segnali, tot_segnali)
                                  //per ogni punto candidato alla posizione

    private Hashtable hashPoints;
    private Hashtable hashPointsToRoom;

    private SelectedPoint elected; //punto, tra tutti quelli candidati, eletto
                                  //alla carica di posizione corrente!

    public ModifierPosition(Vector totalInfoPoint)
    {
        this.totalInfoPoint = totalInfoPoint;

        this.hashPoints = Structurer.hashPoints;
        this.hashPointsToRoom = Structurer.hashPointsToRoom;

        this.elected = new SelectedPoint();

        this.Position();
    }

    private void Position()  //elegge un punto!
    {
        //calcolo
        double maxProb = 0;

        for(int i=0; i<this.totalInfoPoint.size(); i++)
        {
            Vector probabilityPoint = new Vector();
            //inizializzo il vector
            for(int x=0; x<101; x++)
                probabilityPoint.add(x, new Integer(0));
            
            TotalInfoPointPosition onePoint = (TotalInfoPointPosition)this.totalInfoPoint.get(i);

            String point = onePoint.getPoint();
            Vector mac = onePoint.getMac();

            for(int j=0; j<mac.size(); j++)
            {
                Hashtable hashMac = (Hashtable)this.hashPoints.get(point + ".txt");

                String tmpMac = (String)mac.get(j);
                Hashtable hashProbability = (Hashtable)hashMac.get(tmpMac);

                for(int k=0; k<hashProbability.size(); k++)
                {

                    String freq = (String)hashProbability.get(k);
                    Integer intFreq = new Integer(freq);

                    Integer oldFreq = (Integer)probabilityPoint.get(k);

                    Integer newFreq = oldFreq + intFreq;

                    probabilityPoint.set(k, newFreq);
                }
            }

            Integer totalSignalForPoint = (Integer)probabilityPoint.get(100);

            double finalProbPoint = 1;

            for(int z=0; (z<probabilityPoint.size()-1); z++)
            {
                if(onePoint.getSignal(z) > 0)
                {
                    Integer freqSignal = (Integer)probabilityPoint.get(z);
                    double op1 = freqSignal.floatValue();
                   // if(op1 == 0)
                    //  continue;
                    double op2 = totalSignalForPoint.floatValue();
                    double tot1 = (op1/op2);
                    double op3 = this.totalInfoPoint.size();
                    double probSignal = (tot1/op3);
                    //double probSignal = ( freqSignal.floatValue() / totalSignalForPoint.floatValue() )* (1 /totalInfoPoint.size() );

                    finalProbPoint = ( finalProbPoint * probSignal );
                }
            }

            if(finalProbPoint > maxProb)
            {
                maxProb = finalProbPoint;
                this.elected.setPoint(point);
                this.elected.setProbability(finalProbPoint);
            }
        }

        Calendar calendar = new GregorianCalendar();
        int ore = calendar.get(Calendar.HOUR);
        int minuti = calendar.get(Calendar.MINUTE);
        int secondi = calendar.get(Calendar.SECOND);

        String room = (String)this.hashPointsToRoom.get(elected.getPoint());
        System.out.println("Punto selezionato: " + elected.getPoint());
        System.out.println("Stanza selezionata: " + room);
        System.out.println(ore + ":" + minuti + ":" + secondi);

        UpdaterVirtuosoPosition updater = new UpdaterVirtuosoPosition(this.elected.getPoint(), room);
        updater.update();
    }

}
